package com.systop.dxjj.modules.teacher.service.impl;

import com.systop.dxjj.common.pojo.ResponseResult;
import com.systop.dxjj.common.pojo.Student;
import com.systop.dxjj.common.pojo.Teacher;
import com.systop.dxjj.common.pojo.TeacherUser;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.teacher.mapper.TeacherMapper;
import com.systop.dxjj.modules.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    @Qualifier("teaauthenticationManagerBean")
    private AuthenticationManager teaauthenticationManager;

    @Override
    public R selectAllTeachs() {
        List<Teacher> list = teacherMapper.selectAllTeachs();
        return R.ok().put("teachers",list);
    }

    @Override
    public R loginTeachs(Teacher teacher) {
        Teacher t = teacherMapper.loginTeach(teacher);
        if (t == null) {
            return R.ok().put("start","0");
        }else {
            return R.ok().put("start","1");
        }
    }

    @Override
    public R selectTeachById(Integer id) {
        Teacher teacher = teacherMapper.selectTeachById(id);
        return R.ok().put("teachers",teacher);
    }


    @Override
    public R selectTeachByName(String name) {
        List<Teacher> teacher = teacherMapper.selectStudentByName(name);
        return  R.ok().put("teacher",teacher);
    }

    @Override
    public R selectTeachByVagueName(String name) {
        List<Teacher> teacher = teacherMapper.selectStudentByVagueName(name);
        return  R.ok().put("teacher",teacher);
    }

    @Override
    public R insertTeach(Teacher teacher) {
        int num = teacherMapper.insertTeach(teacher);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public R insertTeach2(Teacher teacher) {
        int num = teacherMapper.insertTeach2(teacher);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public R deleteTeach(Integer id) {
        int num = teacherMapper.deleteTeach(id);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public R updateTeach(Teacher teacher) {
        int num = teacherMapper.updateTeach(teacher);
        if(num>0) {
//            return R.ok().put("msg","添加成功");
            return  R.ok().put("start","1");
        }else {
//            return R.ok().put("msg","添加失败");
            return R.ok().put("start","0");
        }
    }

    @Override
    public ResponseResult teachLogin(Teacher teacher) {
        //进行用户认证
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(teacher.getTeachphone(),teacher.getTeachpassword());
        teaauthenticationManager.authenticate(usernamePasswordAuthenticationToken);


        Authentication authenticate = teaauthenticationManager.authenticate(usernamePasswordAuthenticationToken);
        //如果认证没通过给出提示
        if (Objects.isNull(authenticate)){
            throw new RuntimeException("登录失败");
        }
        //如果认知通过，使用teachid生成jwt，jwt存入ResponseResult返回
        TeacherUser teacherUser = (TeacherUser)authenticate.getPrincipal();
        String teaid = teacherUser.getTeacher().getTeachid().toString();
        String jwt = JwtUtil.createJWT(teaid);
        //把完整的用户信息存入redis，userid作为key
        Map<String, String> map = new HashMap<>();
        map.put("token",jwt);
        //把完整的用户信息存入redis，userid作为key
        redisCache.setCacheObject("login:"+teaid,teacherUser);
        return new ResponseResult(200,"登陆成功！",map);
    }

    @Override
    public ResponseResult teachlogout() {
        //获取SecurityContextHolder中的用户id
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        TeacherUser teacherUser = (TeacherUser) authentication.getPrincipal();
        Integer teaid = teacherUser.getTeacher().getTeachid();
        //删除redis中的值
        redisCache.deleteObject("login:"+teaid);
        return new ResponseResult(200,"注销成功");
    }
}
